/**
 * @fileoverview 统一路由管理工具
 * @description 提供 navigateTo、switchTab、redirectTo 的统一封装
 */

class Router {
  /**
   * 页面跳转
   * @param {string} url - 目标页面路径
   * @param {Object} params - 页面参数
   */
  static navigateTo(url, params = {}) {
    // tabBar 页面路径
    const tabBarPages = [
      '/pages/home/index',
      '/pages/community/index',
      '/pages/teacher/index',
      '/pages/buddy/index',
      '/pages/user/index'
    ]
    let fullUrl = url
    if (Object.keys(params).length > 0) {
      const queryString = Object.entries(params)
        .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
        .join('&')
      fullUrl = `${url}?${queryString}`
    }
    // 如果跳转到 tabBar 页面，自动用 switchTab
    if (tabBarPages.includes(url)) {
      return new Promise((resolve, reject) => {
        wx.switchTab({
          url,
          success: resolve,
          fail: reject
        })
      })
    }
    return new Promise((resolve, reject) => {
      wx.navigateTo({
        url: fullUrl,
        success: resolve,
        fail: reject
      })
    })
  }

  /**
   * 切换到 tabBar 页面
   * @param {string} url - 目标页面路径
   */
  static switchTab(url) {
    return new Promise((resolve, reject) => {
      wx.switchTab({
        url,
        success: resolve,
        fail: reject
      })
    })
  }

  /**
   * 页面重定向
   * @param {string} url - 目标页面路径
   * @param {Object} params - 页面参数
   */
  static redirectTo(url, params = {}) {
    // tabBar 页面路径
    const tabBarPages = [
      '/pages/home/index',
      '/pages/community/index',
      '/pages/teacher/index',
      '/pages/buddy/index',
      '/pages/user/index'
    ]
    let fullUrl = url
    if (Object.keys(params).length > 0) {
      const queryString = Object.entries(params)
        .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
        .join('&')
      fullUrl = `${url}?${queryString}`
    }
    // 如果跳转到 tabBar 页面，自动用 switchTab
    if (tabBarPages.includes(url)) {
      return new Promise((resolve, reject) => {
        wx.switchTab({
          url,
          success: resolve,
          fail: reject
        })
      })
    }
    return new Promise((resolve, reject) => {
      wx.redirectTo({
        url: fullUrl,
        success: resolve,
        fail: reject
      })
    })
  }

  /**
   * 返回上一页或多级页面
   * @param {number} delta - 返回的页面数，如果 delta 大于现有页面数，则返回到首页
   */
  static navigateBack(delta = 1) {
    return new Promise((resolve, reject) => {
      wx.navigateBack({
        delta,
        success: resolve,
        fail: reject
      })
    })
  }

  /**
   * 重新加载
   * @param {string} url - 目标页面路径
   * @param {Object} params - 页面参数
   */
  static reLaunch(url, params = {}) {
    let fullUrl = url
    if (Object.keys(params).length > 0) {
      const queryString = Object.entries(params)
        .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
        .join('&')
      fullUrl = `${url}?${queryString}`
    }

    return new Promise((resolve, reject) => {
      wx.reLaunch({
        url: fullUrl,
        success: resolve,
        fail: reject
      })
    })
  }

  /**
   * 获取当前页面路径
   */
  static getCurrentPath() {
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    return currentPage ? currentPage.route : ''
  }

  /**
   * 获取当前页面参数
   */
  static getCurrentOptions() {
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    return currentPage ? currentPage.options : {}
  }
}

module.exports = Router
